package com.herui.ota.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.herui.ota.dao.mysql.DeviceOtaHelpMapper;
import com.herui.ota.entity.Device;
import com.herui.ota.entity.DeviceOtaHelp;
import com.herui.ota.enums.ResponseEnum;
import com.herui.ota.exception.ServiceException;
import com.herui.ota.pojo.form.DeviceOtaHelpFilterForm;
import com.herui.ota.service.DeviceOtaHelpService;
import com.mlnx.common.form.PageForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2024-01-13 21:16:03
 */ 
@Service
public class DeviceOtaHelpServiceIml extends BaseService<DeviceOtaHelpMapper, DeviceOtaHelp> implements DeviceOtaHelpService{

	@Autowired
	@Override
	public void setBaseMapper(DeviceOtaHelpMapper baseMapper) {
		super.setBaseMapper(baseMapper);
	}

	@Override
	public IPage page(PageForm pageForm, Object filter) throws IllegalAccessException {

		DeviceOtaHelpFilterForm filterForm = (DeviceOtaHelpFilterForm) filter;
		QueryWrapper<DeviceOtaHelp> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isEmpty(filterForm.getType())){
			queryWrapper.groupBy("type");
			IPage page = this.page(new Page(pageForm.getCurrent(), pageForm.getSize()), queryWrapper);
			return page;
		}else{
			return super.page(pageForm, filter);
		}
	}

	@Override
	public void save(DeviceOtaHelp entity) {
		checkRight("type_edit");
		super.save(entity);
	}

	@Override
	public boolean updateById(DeviceOtaHelp entity) {
		checkRight("type_edit");
		return super.updateById(entity);
	}

	@Override
	public boolean removeById(Serializable id) {
		checkRight("type_edit");
		return super.removeById(id);
	}

	@Override
	public boolean removeByIds(Collection<? extends Serializable> idList) {
		checkRight("type_edit");
		return super.removeByIds(idList);
	}

	@Override
	public IPage getListType(PageForm pageForm) {

		QueryWrapper<DeviceOtaHelp> queryWrapper = new QueryWrapper<>();
		queryWrapper.groupBy("type");

		IPage page = this.page(new Page(pageForm.getCurrent(), pageForm.getSize()), queryWrapper);
		return page;
	}

	@Override
	public List<DeviceOtaHelp> getDeviceOtaHelps(String channel, Integer otaId) {
		return baseMapper.findDeviceOtaHelps(channel, otaId);
	}
}

